package hot_100;

import java.util.ArrayList;
import java.util.List;

public class 二叉树的右视图 {
    public static void main(String[] args) {

    }

    ArrayList<Integer> ans = new ArrayList<>();

    public List<Integer> rightSideView(TreeNode root) {
        if (root == null)
            return ans;
        Dfs(root, 1);
        return ans;
    }

    void Dfs(TreeNode root, int depth) {
        if (root == null)
            return;
        if (ans.size() == depth)
            ans.add(root.val);
        Dfs(root.right, depth + 1);
        Dfs(root.left, depth + 1);
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
